<?php
declare (strict_types = 1);

namespace app\middleware;

use think\facade\Log;
use think\facade\Cache;
class RepeatRequest
{
    /**
     * 处理请求参数
     * 重复请求中间件 处理频繁请求
     * 添加接口重复请求处理
     */
    public function handle($request, \Closure $next)
    {
        if(in_array($request->action(),['create','test37','integral_pay'])) {
            $key = $request->ip() . '_' . $request->controller() . '/' . $request->action();
            $cache = Cache::get($key);
            Cache::set($key, 'repeat_request', 1);
            if ($cache) {
                abort(json(['code' => 400, 'msg' => '请求过于频繁']));
            }
        }
        return $next($request);
    }

}
